import random

class MagicStudent:
    def __init__(self, name, magic_power, spell_list):
        self.name = name
        self.magic_power = magic_power
        self.spell_list = spell_list
        self.health = 100
        self.silenced = False  # 是否被沉默
        self.stunned = False  # 是否被眩晕

    def cast_spell(self, other_student, students):
        if self.silenced:
            print(f"{self.name} 被沉默了，无法施法！")
            self.silenced = False
            return
        if self.stunned:
            print(f"{self.name} 被眩晕了，无法行动！")
            self.stunned = False
            return

        spell = random.choice(self.spell_list)
        if spell == "护盾咒":
            # 为自身恢复生命值
            heal_power = self.magic_power * random.uniform(0.8, 1.2)
            self.health += heal_power
            print(f"{self.name} 使用了护盾咒，恢复了 {heal_power:.2f} 点生命值！")
            print(f"{self.name} 当前生命值为 {self.health:.2f}。")
        elif spell == "治疗咒":
            # 为随机一个存活的队友恢复生命值
            allies = [student for student in students if student.health > 0 and student != self]
            if allies:
                ally = random.choice(allies)
                heal_power = self.magic_power * random.uniform(0.8, 1.2)
                ally.health += heal_power
                print(f"{self.name} 使用了治疗咒，为 {ally.name} 恢复了 {heal_power:.2f} 点生命值！")
                print(f"{ally.name} 当前生命值为 {ally.health:.2f}。")
        elif spell == "沉默咒":
            # 使目标在下一轮无法施法
            print(f"{self.name} 对 {other_student.name} 施放了沉默咒！")
            other_student.silenced = True
        elif spell == "眩晕咒":
            # 使目标在下一轮无法行动
            print(f"{self.name} 对 {other_student.name} 施放了眩晕咒！")
            other_student.stunned = True
        elif spell == "群体攻击咒":
            # 对所有敌人造成少量伤害
            print(f"{self.name} 施放了群体攻击咒！")
            for student in students:
                if student != self and student.health > 0:
                    spell_power = self.magic_power * random.uniform(0.3, 0.5)
                    student.health -= spell_power
                    print(f"{self.name} 对 {student.name} 造成了 {spell_power:.2f} 点群体伤害！")
                    if student.health <= 0:
                        print(f"{student.name} 被打败了！")
                    else:
                        print(f"{student.name} 还剩下 {student.health:.2f} 点生命值。")
        elif spell == "反弹咒":
            # 将下一次受到的伤害反弹给攻击者
            print(f"{self.name} 施放了反弹咒！")
            self.shield = True
        else:
            # 普通攻击咒语
            spell_power = self.magic_power * random.uniform(0.8, 1.2)
            other_student.health -= spell_power
            print(f"{self.name} 对 {other_student.name} 施展了 {spell}，造成了 {spell_power:.2f} 点伤害！")
            if other_student.health <= 0:
                print(f"{other_student.name} 被打败了！")
            else:
                print(f"{other_student.name} 还剩下 {other_student.health:.2f} 点生命值。")

def display_students(student_pool):
    print("\n可选学生列表：")
    for idx, student in enumerate(student_pool, start=1):
        print(f"{idx}. {student.name} (魔法强度: {student.magic_power}, 法术: {', '.join(student.spell_list)})")

def pick_student(student_pool, team_name):
    display_students(student_pool)
    while True:
        try:
            choice = int(input(f"{team_name} 请选择一个学生（输入编号）："))
            if 1 <= choice <= len(student_pool):
                return student_pool.pop(choice - 1)
            else:
                print("无效的编号，请重新选择。")
        except ValueError:
            print("请输入一个有效的数字。")

def magic_battle(team1, team2):
    print("\n魔法对决开始！")
    round = 1
    while any(student.health > 0 for student in team1) and any(student.health > 0 for student in team2):
        print(f"\n第 {round} 回合：")
        # 红队攻击
        for attacker in team1:
            if attacker.health > 0:
                targets = [student for student in team2 if student.health > 0]
                if targets:
                    target = random.choice(targets)
                    attacker.cast_spell(target, team1)
        # 蓝队攻击
        for attacker in team2:
            if attacker.health > 0:
                targets = [student for student in team1 if student.health > 0]
                if targets:
                    target = random.choice(targets)
                    attacker.cast_spell(target, team2)
        round += 1

    if any(student.health > 0 for student in team1):
        print("红队获胜！")
    else:
        print("蓝队获胜！")

# 创建学生池
student_pool = [
    MagicStudent("student1", 25, ["火焰咒", "冰冻咒", "闪电咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"]),
    MagicStudent("student2", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"]),
    MagicStudent("student3", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"]),
    MagicStudent("student4", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"]),
    MagicStudent("student5", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"]),
    MagicStudent("student6", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"]),
    MagicStudent("student7", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"]),
    MagicStudent("student8", 25, ["火焰咒", "变形咒", "护盾咒", "治疗咒", "沉默咒", "眩晕咒", "群体攻击咒", "反弹咒"]),
]

# 分队
team_size = 4
team1 = []
team2 = []

print("红队和蓝队交替选择学生：")
for i in range(team_size):
    student = pick_student(student_pool, "红队")
    team1.append(student)
    if student_pool:  # 确保学生池中还有学生
        student = pick_student(student_pool, "蓝队")
        team2.append(student)

print("\n红队成员：", [student.name for student in team1])
print("蓝队成员：", [student.name for student in team2])

# 开始魔法对决
magic_battle(team1, team2)